<?php

namespace app\api\model;

use think\Model;

/**
 * token模型
 */
class Token extends Model
{

    protected $name = 'user_token';

    // 开启自动写入时间戳字段
    protected $auto_timestamp = 'false';


    public function setToken($token, $uid, $type)
    {

        $is_exit = $this->where('user_id', $uid)->where('device_type', $type)->find();
        $expire_time = time() + 86400; // 默认过期时间一天
        if ($is_exit) {
            $rst = $this->where('user_id', $uid)->where('device_type', $type)->update(['token' => $token, 'expire_time' => $expire_time]);
        } else {
            $rst = $this->save(['user_id' => $uid, 'device_type' => $type, 'token' => $token,  'expire_time' => $expire_time]);
        }

        return $rst;
    }
}
